﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
namespace Shorten.Core
{
    public class Utility
    {
        public static string GetRandomShortenUrl(int length)
        {
            if (length == -1)
                length = GetRandomNumber(2, 6);

            string charPool = "ABCDEFGOPQRSTUVWXY1234567890ZabcdefghijklmHIJKLMNnopqrstuvwxyz";
            StringBuilder rs = new StringBuilder();
            Random random = new Random();

            for (int i = 0; i < length; i++)
            {
                rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
            }
            return rs.ToString();
        }

        public static string GetSiteUrl()
        {
            if(!String.IsNullOrEmpty(HttpContext.Current.Request.Url.Authority))
                return string.Format("{0}://{1}/", "http",HttpContext.Current.Request.Url.Authority);
            else
                return string.Format("{0}://{1}/", "http", HttpContext.Current.Request.Url.Host);
        }

        public static int GetRandomNumber(int start, int end)
        {
            Random rand = new Random();
            return rand.Next(start, end);
        }
    }
}
